package com.fc.redis.controller;

import com.alibaba.fastjson.JSONObject;
import com.fc.redis.entity.EPriceType;
import com.fc.redis.mapper.TbUserMapper;
import com.fc.redis.service.AService;
import com.fc.redis.service.PriceService;
import com.fc.redis.service.TestService;
import lombok.Data;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.ApplicationContext;
import org.springframework.http.*;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * @Author: Administrator.
 * @Description: TODO()
 * @Date:Created in 2019/10/8 0008.
 * @Modified By:
 */
@RestController
public class TestController {

    @Autowired
    private TestService testService;

    @GetMapping("test/add/user")
    public String  testAddUser() {
        testService.addUser();
        return "ok";
    }
    @Resource(name = "aService")
    private AService aService;

    @Autowired
    private ApplicationContext applicationContext;

    @GetMapping("test/add/user2")
    public String  testAddUser2() {
        TestService testService2 = new TestService();
//        Object tbUserMapper = applicationContext.getBean("tbUserMapper");
//        testService2.setTbUserMapper((TbUserMapper) tbUserMapper); // 不推荐！
        testService2.addUser();
        return "ok";
    }

    private static TestService testService3 = new TestService();
    // 这种方式 也是 错误的 同样空指针
    // @Bean 将 对象 交给 IOC 管理
    @GetMapping("test/add/user3")
    public String  testAddUser3() {
        testService3.addUser();
        return "ok";
    }


    @Value("${testkey}")
    private String test;

    final String s = "final string "+ test;

    String s2 = "s2 "+ test;


    @Autowired
    private PriceService priceService;

    private PriceService priceService2 = priceService;

    @RequestMapping("/test/price")
    public String testE(String prieType) {
//        priceService.testPrice(EPriceType.OrderPrice);
        EPriceType ePriceType = EPriceType.of(prieType);
        return priceService.testPrice(ePriceType);
//        if("Order".equals(prieType)){
//            return priceService.testPrice(EPriceType.OrderPrice);
//        }
//        if("Product".equals(prieType)){
//            return priceService.testPrice(EPriceType.ProductPrice);
//        }
//        return null;
    }




    @Autowired
    private RestTemplate restTemplate;
    @PostMapping("/get/user")
    public User getUser(@RequestBody User user, HttpServletRequest request) {
        String token = request.getHeader("token");
        System.out.println(token);
        User newUser = new User();
        newUser.setId(110);
        newUser.setName("警察");
        BeanUtils.copyProperties(newUser, user, "desc", "note");
        return user;
    }
    @RequestMapping("/test/restTemplate")
    public User testTemplate() {
        String url = "http://localhost:9999/get/user";
        User user = new User();
        user.setDesc("desc");
        user.setNote("note");
        // 方式一
        HttpHeaders headers = new HttpHeaders();
        MediaType type = MediaType.parseMediaType("application/json; charset=UTF-8");
        headers.setContentType(type);
        headers.add("token","123456");
        HttpEntity<String> request = new HttpEntity<String>(JSONObject.toJSON(user).toString(), headers);
        User u = restTemplate.postForObject(url, request, User.class);
        /*
        // 方式二 不需要传HttpHeaders的情况使用
        ResponseEntity<User> userResponseEntity = restTemplate.postForEntity(url, user, User.class);
        HttpStatus statusCode = userResponseEntity.getStatusCode();
        boolean xxSuccessful = statusCode.is2xxSuccessful();
        if(xxSuccessful){
            return userResponseEntity.getBody();
        }
        */
        return u;
    }

}

@Data
class User {
    private Integer id;
    private String name;
    private String desc;
    private String note;
}
